/*
 * Copyright (C) 2012 Evgeniy Egorov
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ru.apertum.diasled.cfg;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import java.awt.Color;
import java.util.LinkedList;

/**
 *
 * @author Evgeniy Egorov
 */
public class ColorScale {

    /**
     * Количество делений в шкале
     */
    final public static int DIVISIONS = 16;

    public ColorScale() {
    }
    @Expose
    @SerializedName("min")
    private int min = 0;

    public int getMin() {
        return min;
    }

    public void setMin(int min) {
        this.min = min;
    }
    @Expose
    @SerializedName("max")
    private int max = 2000;

    public int getMax() {
        return max;
    }

    public void setMax(int max) {
        this.max = max;
    }
    @Expose
    @SerializedName("colors")
    private LinkedList<Integer> colors = new LinkedList<>();

    public LinkedList<Integer> getColors() {
        if (colors.size() == 0) {
            int k = ColorScale.DIVISIONS / 5;
            for (int value = 1; value <= ColorScale.DIVISIONS; value++) {
                int rowIndex = (max - value) / (max / ColorScale.DIVISIONS);
                int g = (int) Math.round(255 - Math.abs(255 / (ColorScale.DIVISIONS - 2.5 * k) * (-ColorScale.DIVISIONS / 2 + rowIndex)));
                g = g < 0 ? 0 : g;
                int r = 255 - rowIndex * (255 / (ColorScale.DIVISIONS - k));
                r = r < 0 ? 0 : r;
                int b = (rowIndex - k) * (255 / (ColorScale.DIVISIONS - k));
                b = b < 0 ? 0 : b;
                colors.add(new Color(r, g, b).getRGB());
            }
        }
        return colors;
    }

    public void setColors(LinkedList<Integer> colors) {
        this.colors = colors;
    }
    private final static int STEP = 10;

    public void incMax() {
        max += STEP;
    }

    public void decMax() {
        max -= STEP;
    }

    public void incMin() {
        min += STEP;
    }

    public void decMin() {
        min -= STEP;
    }

    /**
     * Получение цветовой индикации.
     * @param value величина для перевода в цвет
     * @return цвет величины по шкале.
     */
    public Color getColor(int value) {
        if (value > max) {
            return new Color(getColors().get(0));
        }
        if (value < min) {
            return new Color(getColors().get(DIVISIONS - 1));
        }
        int rowIndex = (max - value) / ((max - min) / DIVISIONS);
        return new Color(getColors().get(rowIndex > 15 ? 15 : rowIndex));
    }
}
